// 初始化分类页面
function initUser() {
    show(1, 15);
    setUserModal();
}

// 处理分页的页数
function userPageItem(e) {
    var targetPage = $(e).val();
    var pageLink = $('#pageLink');
    var getPageNum = pageLink.data("page").pageNum;
    var maxPage = pageLink.data("page").pages;
    if (targetPage > 0) {
        if (getPageNum == targetPage) {
            return;
        }
        show(targetPage);
        return;
    }
    if (targetPage == 0) {
        targetPage = 1;
    }
    var finalPage = targetPage + getPageNum;
    if (finalPage < 1 || finalPage > maxPage) {
        return;
    }
    show(finalPage);
}

// 表单回显数据
function updateUserModal(e) {
    var getData = idGetUser($(e).val());
    $('#userNickName').val(getData.data.nickName);
    $('#userId').val(getData.data.id);
}


// 设置表单的值
function setUserModal() {
    itemModal.addEventListener('show.bs.modal', event => {
        document.querySelector('#userForm').reset();
    });
}

// 渲染分页查询的用户
function show(pageNum, pageSize) {

    var tbody = $('#tbody');
    var pageLink = $('#pageLink');
    if (!pageNum) {
        pageNum = pageLink.data('page').pageNum;
    }
    if (!pageSize) {
        pageSize = pageLink.data('page').pageSize;
    }

    var getData = pageUser(pageNum, pageSize);

    if (pageNum > getData.data.pages && pageNum != 1) {
        show(getData.data.pages, pageSize);
        return;
    }

    tbody.children().remove();
    var index = 1;
    getData.data.records.forEach(item => {
        const row = '<tr>\n' +
            '                            <th scope="row">' +
            '                               <input class="checkBox form-check-input" type="checkbox" id="check" name="option1" value="' + item.id + '">\n' +
            '                           </th>     ' +
            '                            <td><span class="d-flex justify-content-center"> ' + index + '</span></td>' +
            '                            <td><span class="d-flex justify-content-center">' + item.nickName + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.phone + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.updateTime.replace("T", " ") + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.createTime.replace("T", " ") + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">\n' +
            '                                <span class="dropdown">\n' +
            '                                  <button class="btn dropdown-toggle" data-bs-boundary="viewport"\n' +
            '                                          data-bs-toggle="dropdown">操作</button>\n' +
            '                                    <ul class="dropdown-menu "\n' +
            '                                        style="min-width: 1px;">\n' +
            '                                        <li>\n' +
            '                                            <button class="dropdown-item btn btn-primary" data-bs-toggle="modal" data-bs-target="#itemModal" data-bs-type="edit" onclick="updateUserModal(this)"  type="button" value="' + item.id + '">编辑</button>\n' +
            '                                        </li>\n' +
            '                                        <li>\n' +
            '                                            <button class="dropdown-item" type="button" onclick="deleteUser(this)" value="' + item.id + '">删除</button>\n' +
            '                                        </li>\n' +
            '                                    </ul>\n' +
            '                                </span>\n' +
            '                             </span>' +
            '                           </td>\n' +
            '                        </tr>';
        tbody.append(row);
        index++;
    });

    pageLink.data("page", {
        pages: getData.data.pages,
        pageNum: pageNum,
        pageSize: pageSize
    });
    // 设置选择头
    $('#checkHead').prop('checked', false);
    // 设置左下角字体
    setPageFront( (pageNum - 1) * pageSize + 1,pageNum * pageSize > getData.data.total ? getData.data.total: pageNum * pageSize, getData.data.total);
    // 加载分页栏
    loadPagingLabel(pageLink);
    // 设置分页栏
    setPagingLabel(pageLink);
    $('td').addClass('align-middle');
    $('.page-item').on('click', function () {
        userPageItem(this);
    });
}

